package com.xushi.service.impl;

import com.xushi.bean.State;
import com.xushi.dao.ActivityRepository;
import com.xushi.model.Activity;
import com.xushi.model.UserTemplate;
import com.xushi.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service("activityService")
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityRepository activityDao;

    public Activity startActivity(String username, UserTemplate userTemplate, Timestamp startTime, Timestamp endTime) {
        Activity activity = new Activity();
        activity.setUserTemplate(userTemplate);
        activity.setStartTime(startTime);
        activity.setEndTime(endTime);
        activity.setState(State.ACTIVE.getState());
        activity.setUsername(username);
        return activityDao.save(activity);
    }

    @Override
    public Activity getActivityById(int activityId) {
        return activityDao.getActivityById(activityId);
    }

    @Override
    public List<Activity> getActiveActivitiesByUT(UserTemplate userTemplate) {
        return activityDao.getActivitiesByUserTemplateAndState(userTemplate, State.ACTIVE.getState());
    }

    @Override
    public Activity updateActivity(Activity activity) {
        return activityDao.save(activity);
    }

    @Override
    public List<Activity> getActiveActivities() {
        List<Activity> activities = new ArrayList<>();

        Iterable<Activity> iterable =  activityDao.findAll();
        iterable.forEach(x -> activities.add(x));

        return activities;
    }
}
